package com.jie.deletefile.zahuo;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jie.deletefile.R;

import java.util.ArrayList;
import java.util.List;

public class SRActivity extends AppCompatActivity {
    private static List<String> datas;
    private SwipeRecyclerView srv;
    private RelativeLayout activity_sr;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sr);
        initData();
        initView();
    }

    private void initData() {
        datas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            datas.add("条目" + i);
        }
    }

    private void initView() {
        srv = (SwipeRecyclerView) findViewById(R.id.srv);
        activity_sr = (RelativeLayout) findViewById(R.id.activity_sr);
        srv.getSwipeRefreshLayout().setColorSchemeColors(Color.RED);
        srv.getRecyclerView().setLayoutManager(new LinearLayoutManager(this));
        adapter = new MyAdapter(this);
        srv.setAdapter(adapter);
        srv.setOnLoadListener(new SwipeRecyclerView.OnLoadListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        datas.clear();
                        for (int i = 0; i < 10; i++) {
                            datas.add(String.valueOf(i));
                        }

                        srv.complete();
                        adapter.notifyDataSetChanged();

                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {

            }
        });
    }

    static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        Context context;
        ItemClick itemClick;


        public MyAdapter(Context context) {
            this.context = context;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_src, parent, false);

            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText(datas.get(position));
            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    itemClick.onclick();
                }
            });

        }


        @Override
        public int getItemCount() {
            return datas.size();
        }

        public interface ItemClick {
            void onclick();

        }

        public void setItemClick(ItemClick itemClick) {
            this.itemClick = itemClick;
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.sc_tv);
        }
    }
}
